package name.fowler.refactoring.simplestore.service;

import name.fowler.refactoring.simplestore.dao.CustomerDao;
import name.fowler.refactoring.simplestore.dao.CustomerNotFoundException;
import name.fowler.refactoring.simplestore.model.Customer;

public class ReportService {

    private CustomerDao customerDao;
    private TextReportFormatter textReportFormatter;
    private HtmlReportFormatter htmlReportFormatter;

    public ReportService(CustomerDao customerDao) {
        this.customerDao = customerDao;
        textReportFormatter = new TextReportFormatter();
        htmlReportFormatter = new HtmlReportFormatter();
    }

    public String buildTextCustomerReport(String name) throws ReportException {
        Customer customer;
        try {
            customer = customerDao.findByName(name);
        } catch (CustomerNotFoundException ex) {
            throw new ReportException(ex);
        }
        return textReportFormatter.format(customer);
    }

    public String buildHtmlCustomerReport(String name) throws ReportException {
        Customer customer;
        try {
            customer = customerDao.findByName(name);
        } catch (CustomerNotFoundException ex) {
            throw new ReportException(ex);
        }
        return htmlReportFormatter.format(customer);
    }
}
